home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / em-xmkit.zip / LOADCODE.ASM < prev    next >
Assembly Source File  |  1989-11-28  |  12KB  |  285 lines

  1. NAME     Loadcode
  2. codeseg    segment 'code'
  3.  
  4. ;-----------------------------------------------------------------------------;
  5. ; This is an example assembly language program designed to show how to        ;
  6. ; use the DOS load overlay function to load a portion of code in expanded     ;
  7. ; memory and then how to use EMM to call it.                                  ;
  8. ;                                                                             ;
  9. ; The major steps it follows are:                                             ;
  10. ;       Test for presence of EMM                                              ;
  11. ;       Allocate a page of expanded memory                                    ;
  12. ;       Map the page in                                                       ;
  13. ;       Use DOS load overlay to load the program HELLO.EXE to the mapped page ;
  14. ;       Use EMS map_and_call to start execution of HELLO.EXE                  ;
  15. ;       Deallocate the page                                                   ;
  16. ;       Terminate                                                             ;
  17. ;                                                                             ;
  18. ; The "overlay" code that gets executed out of expanded memory MUST follow    ;
  19. ; strict guidelines:                                                          ;
  20. ;       The Stack MUST be left in conventional memory                         ;
  21. ;       A "RETF" far return should end the code so that the kernal in         ;
  22. ;               conventional memory can deallocate the EMS memory             ;
  23. ;                                                                             ;
  24. ; Executing code out of expanded memory is best done in assembly language.    ;
  25. ; High level languages (even C) don't offer enough control of where the       ;
  26. ; stack is and how the "overlay" file gets built.  Note that even though      ;
  27. ; HELLO.EXE has been linked into an "EXE" file it is NOT actually executable  ;
  28. ; from the DOS prompt.                                                        ;
  29. ;-----------------------------------------------------------------------------;
  30. ASSUME    CS:codeseg, DS:codeseg, ES:codeseg
  31.  
  32. ;-----------------------------------------------------------------------------;
  33. ;          E Q U A T E S                                                      ;
  34. ;-----------------------------------------------------------------------------;
  35. PAGE;
  36. ;======================================;
  37. ;    Equates used by the EMM        ;
  38. ;======================================;
  39.  
  40.     ;-----------------------------------------------------;
  41.     ; define the enhanced memory manager access interrupt ;
  42.     ;-----------------------------------------------------;
  43. EMM        EQU    67H        ;interrupt to use to access EMM functions
  44.  
  45.     ;-----------------------------------------;
  46.     ; define the first page in the page frame ;
  47.     ; nad the logical page we will use.       ;
  48.     ;-----------------------------------------;
  49. phys_page_0    EQU    0
  50. log_page_0    EQU    0
  51.  
  52.     ;------------------------------------------;
  53.     ; define enhanced memory manager functions ;
  54.     ;------------------------------------------;
  55. GetEMMStatus        EQU    40h    ;function used to check for EMM present
  56. GetPageFrame        EQU    41h    ;function used to get the page frame address
  57. AllocatePages        EQU    43h    ;function used to get a pID and allocate pages
  58. MapPage            EQU    44h    ;function used to map a memory page into the page frame
  59. DeallocatePages     EQU    45h    ;function used to deallocate pages
  60.  
  61. ;======================================;
  62. ;    Equates used for DOS           ;
  63. ;======================================;
  64. Load_overlay            EQU    4B03H    ;function used to load overlay
  65. DOS_print_string    EQU    09H    ;DOS print function
  66. DOSTerminate        EQU    4CH    ;Dos terminate function
  67.  
  68.  
  69. PAGE
  70. ;=======================================================================;
  71. ;                                    ;
  72. ;  Procedure test_for_EMM                        ;
  73. ;                                    ;
  74. ;  Description:  This procedure tests for the presence of EMM in the    ;
  75. ;         system and will return with the CARRY FLAG SET if the    ;
  76. ;         manager is present, otherwise it will return with the    ;
  77. ;         CARRY FLAG CLEAR.                    ;
  78. ;                                    ;
  79. ;  Parameters:     None                            ;
  80. ;                                    ;
  81. ;  Calling Sequence:  CALL    test_for_EMM                ;
  82. ;              JCS    errorHandler                ;
  83. ;                                    ;
  84. ;  Register usage:  Carry flag set to indicate status returned        ;
  85. ;                                    ;
  86. ;=======================================================================;
  87.  
  88. test_for_EMM    PROC    NEAR
  89.  
  90.         PUSH    ES
  91.  
  92.         MOV    AH, 35h         ;issue "get interrupt vector"
  93.         MOV    AL, 67h
  94.         INT    21H
  95.  
  96.         MOV    DI, 000Ah        ;use the SEGMENT in ES returned
  97.                         ;  by DOS, place the "device
  98.                         ;  name field"  OFFSET in DI
  99.         LEA    SI, device_name     ;place the OFFSET of the
  100.                         ;  EMMXXXX0 name string in SI,
  101.                         ;  SEGMENT is already in DS
  102.         MOV    CX, 8            ;compare the name strings
  103.         REPE    CMPSB
  104.  
  105.         JNE    test_for_EMM_error_exit ;the driver name string:
  106.  
  107. test_for_EMM_exit:
  108.         STC                ;was found, so EMM is present
  109.         POP    ES
  110.         RET
  111.  
  112. test_for_EMM_error_exit:
  113.         CLC                ;was not found, EMM is not present
  114.         POP    ES
  115.         RET
  116.  
  117. device_name:    DB    "EMMXXXX0"
  118.  
  119. test_for_EMM    ENDP
  120.  
  121. PAGE
  122. ;-----------------------------------------------------------------------------;
  123. ;                                                                             ;
  124. ;                                                                             ;
  125. ;-----------------------------------------------------------------------------;
  126.  
  127. loadcode_main    PROC    NEAR
  128.  
  129.     ;---------------------------------------------------------------------;
  130.     ; Set DS, ES segments to our CS                                       ;
  131.     ;---------------------------------------------------------------------;
  132.     MOV    AX, CS
  133.     MOV    ES, AX
  134.     MOV    DS, AX
  135.  
  136.     ;---------------------------------------------------------------------;
  137.     ; Test for EMM's presence                                             ;
  138.     ;---------------------------------------------------------------------;
  139.     CALL    test_for_EMM
  140.     JNC    error                 ;No carry set - EMM not found
  141.  
  142.     ;---------------------------------------------------------------------;
  143.     ; get segment address of the page frame from EMM                      ;
  144.     ;---------------------------------------------------------------------;
  145.     MOV        AH, GetPageFrame    ;function to get address of page frame
  146.     INT        EMM
  147.     OR        AH, AH            ;Test for error return from EMM
  148.     JNZ        error
  149.  
  150.     ;---------------------------------------------------------------------;
  151.     ; save the segment address of the page frame                          ;
  152.     ;---------------------------------------------------------------------;
  153.     MOV        page_frame_addr, BX
  154.  
  155.     ;---------------------------------------------------------------------;
  156.     ; Allocate enough pages to load HELLO.EXE - 1 page should be enough   ;
  157.     ;---------------------------------------------------------------------;
  158.     MOV        AH, AllocatePages    ;function to get a pID and pages assigned
  159.     MOV        BX, 1                  ;number of pages to request
  160.     INT        EMM
  161.     OR        AH, AH            ;Test for error return from EMM
  162.     JNZ        error
  163.  
  164.     ;---------------------------------------------------------------------;
  165.     ; save the handle returned by EMM                                     ;
  166.     ;---------------------------------------------------------------------;
  167.     MOV        handle, DX 
  168.  
  169.     ;---------------------------------------------------------------------;
  170.     ; map in the logical page at physical page 0                          ;
  171.     ;---------------------------------------------------------------------;
  172.     MOV        DX, handle         ;set up process ID for EMM call
  173.     MOV        BX, log_page_0        ;map in our only logical page
  174.     MOV        AL, phys_page_0        ;designate page 0 in frame
  175.     MOV        AH, MapPage        ;function to map in page
  176.     INT        EMM            ;execute function
  177.     OR        AH, AH            ;Test for error return from EMM
  178.     JNZ        error
  179.  
  180.     ;---------------------------------------------------------------------;
  181.     ; Use DOS overlay function to load HELLO.EXE into expanded memory     ;
  182.     ;---------------------------------------------------------------------;
  183.     PUSH        DS
  184.     POP        ES
  185.     LEA        BX, hello_overlay_info    ;ES:BX --> overlay struct
  186.  
  187.     MOV        AX, page_frame_addr        ;Set overlay info to the
  188.     MOV        ES:[BX].segment_addr, AX       ;page frame segment
  189.     MOV        ES:[BX].relocation_factor, AX
  190.  
  191.     MOV        DX, OFFSET hello_filename    ;DS:DX --> ASCIIZ filename
  192.  
  193.     MOV        AX, Load_overlay        ;Use DOS to load the overlay
  194.     INT        21H
  195.     JC        error
  196.  
  197.     ;---------------------------------------------------------------------;
  198.     ; Set up hello's execution address and call hello                     ;
  199.     ;---------------------------------------------------------------------;
  200.     MOV        AX, page_frame_addr
  201.     MOV        hello_segment, AX
  202.  
  203.     LEA        BX, hello_execution_addr
  204.  
  205.     CALL         DWORD PTR [BX]
  206.  
  207. terminate:    
  208.     MOV        AH, DeallocatePages
  209.     MOV        DX, handle
  210.     INT        EMM
  211.     OR        AH, AH
  212.     JNZ        error
  213.  
  214. terminate_exit:
  215.     MOV        AH, DosTerminate    ;DOS terminate command
  216.     MOV        AL, 0            ;load normal return code
  217.     INT        21h
  218.  
  219. PAGE
  220.     ;=====================================================================;
  221.     ; if there were EMM or DOS errors,                                    ;
  222.     ;     display error message                                           ;
  223.     ;=====================================================================;
  224.  
  225. error:
  226.     LEA        DX, error_msg
  227.     MOV        AH, DOS_print_string
  228.     INT        21h
  229.     JMP        terminate_exit
  230.  
  231. loadcode_main    ENDP
  232.  
  233. PAGE
  234. ;-----------------------------------------------------------------------------;
  235. ;                                          ;
  236. ;        D A T A                                             ;
  237. ;                                          ;
  238. ;-----------------------------------------------------------------------------;
  239.  
  240.     ;=====================================================================;
  241.     ;                        EMM related variables                        ;
  242.     ;=====================================================================;
  243.     ;---------------------------------------------------------------------;
  244.     ; handle returned by EMM.                                             ;
  245.     ;---------------------------------------------------------------------;
  246. handle            DW    ?    ;process ID returned by EMM when pages
  247.                                         ;  are allocated
  248.  
  249.     ;---------------------------------------------------------------------;
  250.     ; address of the EMM page frame                                       ;
  251.     ;---------------------------------------------------------------------;
  252. page_frame_addr        DW    ?        ;segment address of top of page frame
  253.  
  254.     ;---------------------------------------------------------------------;
  255.     ; Structure passed to DOS to load an overlay                          ;
  256.     ;---------------------------------------------------------------------;
  257.     Overlay_struc            STRUC
  258.         segment_addr        DW (?)
  259.         relocation_factor    DW (?)
  260.     Overlay_struc            ENDS
  261.  
  262.     hello_overlay_info        Overlay_struc <>
  263.  
  264.     ;---------------------------------------------------------------------;
  265.     ; Filename of overlay to load                                         ;
  266.     ;---------------------------------------------------------------------;
  267. hello_filename    DB    'Hello.exe', 0
  268.  
  269.     ;---------------------------------------------------------------------;
  270.     ; Address to begin execution of the overlay                           ;
  271.     ;---------------------------------------------------------------------;
  272. hello_execution_addr    LABEL DWORD
  273.     hello_offset    DW    100H     ;Depends on hello.exe
  274.     hello_segment    DW    ?    ;Will be set to page_frame_addr
  275.  
  276.     ;---------------------------------------------------------------------;
  277.     ; Messages to be displayed                                            ;
  278.     ;---------------------------------------------------------------------;
  279. error_msg    DB 'Error encountered.', '$'
  280.  
  281. codeseg     ENDS
  282.         
  283. END    loadcode_main
  284.  
  285.